package light.head.dao.el;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;



import light.head.constant.Parm;
import light.head.dto.ExamTopic;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.el.PaperTemplateItem;
import light.head.model.el.Topic;
@Service
public class TopicDAO extends BaseDAO<Topic, Integer>{
	public RESULT findByMap(Integer oc,Map<String,String>map, PageBean pb) {
		StringBuffer hql = new StringBuffer("FROM Topic t WHERE t.deleted = 0 AND t.oc =?");
		List<Object> list= new ArrayList<Object>();
		list.add(oc);
		if (map != null) {
			String cc = map.get(Parm.CC);
			String tdc = map.get(Parm.TDC);
			String type= map.get(Parm.TYPE);
			String content = map.get(Parm.CONTENT);
			if(isNotNull(tdc)){
				hql.append(" AND t.tdc = ? ");
				list.add(Integer.parseInt(tdc));
			}
			if(isNotNull(cc)){
				hql.append(" AND t.code IN (SELECT tcr.tc FROM TopicCourseRelation tcr WHERE tcr.cc = ? )");
				list.add(Integer.parseInt(cc));
			}
			if(isNotNull(type)){
				hql.append(" AND t.type = ? ");
				list.add(Byte.parseByte(type));
			}
			if(isNotNull(content)){
				hql.append(" AND t.content LIKE ? ");
				list.add("%" + content + "%");
			}
		}
		hql.append(" ORDER BY t.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	/**
	 * 修改
	 */
	public RESULT update(Topic nt){
		if(nt == null || nt.getCode() == null) {
			return RESULT.ERROR;
		}
		Topic old = get(nt.getCode());
		if(old == null){
			return RESULT.NOT_FIND;
		} 
		if(nt.getTdc() != null && nt.getTdc() != old.getTdc()){
			old.setTdc(nt.getTdc());
		}
		if(nt.getType() != null && nt.getType() != old.getType()){
			old.setType(nt.getType());
		}
		if(isNotNull(nt.getContent()) && !nt.getContent().equals(old.getContent())){
			old.setContent(nt.getContent());
		}
		return upd(old);
	}
	
	/**
	 * 根据班级编号及课程编号获取指定的考试题目
	 * clc 班级编号
	 * cc  课程编号
	 */
	public List<ExamTopic> getExamByClcAndCc(Integer clc, Integer cc){
		if(clc == null || cc == null) return null;
		List<Object> parmList = new ArrayList<Object>();
		StringBuffer sqlBuffer = new StringBuffer("SELECT t.code, t.content, t.type, cctr.points FROM Topic t");
		sqlBuffer.append(" JOIN ClassCourseTopicRelation cctr ON cctr.tc = t.code");
		sqlBuffer.append(" WHERE cctr.clc = ? AND cctr.cc = ?");
		parmList.add(clc);
		parmList.add(cc);
		
		final String sql = sqlBuffer.toString();
		final List<Object> parm = parmList;
		
		return this.getHibernateTemplate().execute(new HibernateCallback<List<ExamTopic>>() {

			@Override
			public List<ExamTopic> doInHibernate(Session session)
					throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery(sql);
				for(int i = 0; i < parm.size(); i ++){
					query.setParameter(i, parm.get(i));
				}
				
				@SuppressWarnings("unchecked")
				List<Object[]> result = query.list();
				if(result != null){
					List<ExamTopic> list = new ArrayList<ExamTopic>();
					ExamTopic et = null;
					for(Object[] item : result){
						et = new ExamTopic();
						et.setCode((Integer)item[0]);
						et.setContent(item[1] == null ? null : (String)item[1]);
						et.setType(item[2] == null ? null : (Byte)item[2]);
						et.setPoints(item[3] == null ? null : (Float)item[3]);
						list.add(et);
					}
					return list;
				}
				return null;
			}
		});
	}
	
	/**
	 * 根据试卷模板获取试卷题目
	 * cc 课程编号
	 * tempList 模板
	 * @return
	 */
	public List<ExamTopic> getExamByTempItem(Integer cc, List<PaperTemplateItem> tempList){
		if(cc == null || tempList == null || tempList.size() < 1) return null;
		StringBuffer sqlBuffer = new StringBuffer("SELECT t.code, t.content, t.type FROM Topic t");
		sqlBuffer.append(" JOIN TopicCourseRelation tcr ON t.code = tcr.tc");
		sqlBuffer.append(" WHERE tcr.cc = ? AND t.type = ? AND t.tdc = ? ORDER BY RAND() LIMIT ?");
		List<Object> parmList = null;
		List<ExamTopic> list = new ArrayList<ExamTopic>();
		List<ExamTopic> etaList = null;
		for(PaperTemplateItem temp : tempList){
			if(temp.getNum() > 0){
				parmList = new ArrayList<Object>();
				parmList.add(cc);
				parmList.add(temp.getType());
				parmList.add(temp.getLc());
				parmList.add(temp.getNum());
				
				final String sql = sqlBuffer.toString();
				final List<Object> parm = parmList;
				final Float point = temp.getPoint();
				
				etaList = this.getHibernateTemplate().execute(new HibernateCallback<List<ExamTopic>>() {
	
					@Override
					public List<ExamTopic> doInHibernate(Session session)
							throws HibernateException, SQLException {
						SQLQuery query = session.createSQLQuery(sql);
						for(int i = 0; i < parm.size(); i ++){
							query.setParameter(i, parm.get(i));
						}
						
						@SuppressWarnings("unchecked")
						List<Object[]> result = query.list();
						
						if(result != null){
							List<ExamTopic> tList = new ArrayList<ExamTopic>();
							ExamTopic et = null;
							for(Object[] item : result){
								et = new ExamTopic();
								et.setCode((Integer)item[0]);
								et.setContent(item[1] == null ? null : (String)item[1]);
								et.setType(item[2] == null ? null : (Byte)item[2]);
								et.setPoints(point);
								tList.add(et);
							}
							return tList;
						}
						return null;
					}
				});
				
				if(etaList != null && etaList.size() > 0){
					list.addAll(etaList);
				}
			}	
		}
		return list;
	}
	
	/**
	 * 题目排序
	 */
	public List<ExamTopic> px(List<ExamTopic> etList){
		List<ExamTopic> list = new ArrayList<ExamTopic>();
		for(int i = 1; i <= 3 ;i++){
			for(ExamTopic et : etList){
				if(et.getType() == i){
					list.add(et);
				}
			}
		}
		return list;
	}
	
	/**
	 * 根据考试记录编号获取考题记录
	 * erc  考试记录编号
	 */
	public List<ExamTopic> findByErc(final Integer erc){
		if(erc == null) return null;
		StringBuffer sqlBuffer = new StringBuffer("SELECT t.code, t.content, t.type, tr.point, tr.points FROM TopicRecord tr");
		sqlBuffer.append(" JOIN Topic t ON t.code = tr.tc");
		sqlBuffer.append(" WHERE tr.erc = ? ORDER BY t.type");
		
		final String sql = sqlBuffer.toString();
		
		return this.getHibernateTemplate().execute(new HibernateCallback<List<ExamTopic>>() {

			public List<ExamTopic> doInHibernate(Session session)
					throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery(sql);
				query.setParameter(0, erc);
				
				@SuppressWarnings("unchecked")
				List<Object[]> result = query.list();
				
				if(result != null){
					List<ExamTopic> list = new ArrayList<ExamTopic>();
					ExamTopic et = null;
					for(Object[] item : result){
						et = new ExamTopic();
						et.setCode((Integer)item[0]);
						et.setContent((String)item[1]);
						et.setType((Byte)item[2]);
						et.setPoint(item[3] == null ? 0f : (Float)item[3]);
						et.setPoints(item[4] == null ? 0f : (Float)item[4]);
						list.add(et);
					}
					return list;
				}
				return null;
			}
		});
	}
}
